package com.wwwarehouse.resource_center.fragment.defindeobjectstore;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CardDeskFunctionResponseBean;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.fragment.CommonBasePagerFragment;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.defindeobjectstore.DefinedObjectStoreBean;
import com.wwwarehouse.resource_center.bean.defindeobjectstore.QueryBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.eventbus_event.DefineEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * author ：minifly
 * date: 2017/10/26
 * time: 13:35
 * desc:
 */
@Route(path = ResourceConstant.DEFINED_INVENT)
public class DefinedViewPagerFragment extends CommonBasePagerFragment {

    private String mBusinessId;

    @Override
    public void requestDatas() {
        super.requestDatas();
        showCustomBtn(mActivity.getResources().getString(R.string.new_define));

        if (getArguments() != null) {
            Bundle bundle = getArguments();
            CardDeskFunctionResponseBean.TaskBean taskBean = (CardDeskFunctionResponseBean.TaskBean) bundle.getSerializable(Constant.KEY_CARD_DESK_FUNCTION_DETAILS);
            mBusinessId = taskBean.getBusinessId();
            requestDateMine();
        }
        hideConfirmButton();
    }

    @Override
    public void onRefreshListener() {
        requestDateMine();
    }

    @Override
    public void onLoadSuccess(String s, int i) {
        if (0 == i) {
            try {
                DefinedObjectStoreBean definedObjectStoreBean = JSON.parseObject(s, DefinedObjectStoreBean.class);
                int total = definedObjectStoreBean.getTotal();
                ArrayList<DefinedObjectStoreBean.ListBean> list = (ArrayList<DefinedObjectStoreBean.ListBean>) definedObjectStoreBean.getList();
                Bundle bundle = new Bundle();
                bundle.putLong("businessUnitUkid", Long.parseLong(mBusinessId));
                setData(total, 5, "com/wwwarehouse/resource_center/fragment/defindeobjectstore/DefinedObjectAllobjectFragment", bundle, list);
            } catch (Exception e) {
                LogUtils.showLog(e.toString());
            }
        }

    }

    @Override
    public void onLoadError(String s) {
        LogUtils.showErrLog("" + s);
    }

    public void requestDateMine() {
        Map<String, Object> getInventMap = new HashMap<>();
        //        getInventMap.put("businessUnitUkid", "10005678");
        getInventMap.put("businessUnitUkid", mBusinessId);
        QueryBean queryBean = new QueryBean();
        queryBean.setPage(1);
        queryBean.setSize(5);
        getInventMap.put("query", queryBean);
        loadData(ResourceConstant.GET_INVENTORY_LIST, getInventMap, 0);

    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof DefinedViewPagerFragment)
            mActivity.setTitle("" + getString(R.string.defined_object_store_title));
    }

    @Override
    public void titleBt1Click() {
        super.titleBt1Click();
        Bundle bundle = new Bundle();
        bundle.putString("businessUnitUkid", mBusinessId);
        DefineSelfDealFragment defineSelfDealFragment = new DefineSelfDealFragment();
        defineSelfDealFragment.setArguments(bundle);
        pushFragment(defineSelfDealFragment);
    }

    public void onEventMainThread(DefineEvent event) {
        if (event.isFinish()) {
            requestDateMine();
        }

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
